namespace DotNetNuke.UI.WebControls
{
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Xml;

    public class TreeNode : DNNNode, IStateManager
    {
        internal static readonly string _checkboxIDSufix = "checkbox";
        internal static string _separator = ":";
        private DnnTree m_objDNNTree;
        private TreeNodeCollection m_objNodes;

        public TreeNode()
        {
        }

        public TreeNode(string strText) : base(strText)
        {
        }

        internal TreeNode(Control ctlOwner)
        {
            this.m_objDNNTree = (DnnTree) ctlOwner;
        }

        internal TreeNode(XmlNode objXmlNode, Control ctlOwner) : base(objXmlNode)
        {
            this.m_objDNNTree = (DnnTree) ctlOwner;
        }

        public void Click()
        {
            this.Selected = true;
            this.DNNTree.OnNodeClick(new DNNTreeNodeClickEventArgs(this));
        }

        public void Collapse()
        {
            if (this.HasNodes)
            {
                this.set_CustomAttribute("expanded", "0");
                this.DNNTree.OnCollapse(new DNNTreeEventArgs(this));
            }
        }

        public void Expand()
        {
            if (this.HasNodes)
            {
                this.set_CustomAttribute("expanded", "1");
                this.DNNTree.OnExpand(new DNNTreeEventArgs(this));
            }
        }

        public void LoadViewState(object state)
        {
        }

        public void MakeNodeVisible()
        {
            if (this.Parent != null)
            {
                this.Parent.Expand();
                this.Parent.MakeNodeVisible();
            }
        }

        public virtual void Render(HtmlTextWriter writer)
        {
            this.NodeWriter.RenderNode(writer, this);
        }

        public object SaveViewState()
        {
            return null;
        }

        internal void SetDNNTree(DnnTree objTree)
        {
            this.m_objDNNTree = objTree;
        }

        public void TrackViewState()
        {
        }

        [Bindable(true), PersistenceMode(PersistenceMode.Attribute), DefaultValue("")]
        public string CssClassOver
        {
            get
            {
                return this.CSSClassHover;
            }
            set
            {
                this.CSSClassHover = value;
            }
        }

        [Browsable(false)]
        public DnnTree DNNTree
        {
            get
            {
                return this.m_objDNNTree;
            }
        }

        [DefaultValue(-1), Bindable(true), PersistenceMode(PersistenceMode.Attribute)]
        public int ImageIndex
        {
            get
            {
                if (Strings.Len(this.get_CustomAttribute("imgIdx")) > 0)
                {
                    return IntegerType.FromString(this.get_CustomAttribute("imgIdx"));
                }
                if (this.DNNTree.ImageList.Count > 0)
                {
                    return 0;
                }
                return -1;
            }
            set
            {
                this.set_CustomAttribute("imgIdx", StringType.FromInteger(value));
            }
        }

        [Browsable(false), DefaultValue(false)]
        public bool IsExpanded
        {
            get
            {
                object obj2;
                if (!this.DNNTree.IsDownLevel)
                {
                    string clientVariable = ClientAPI.GetClientVariable(this.m_objDNNTree.Page, this.m_objDNNTree.ClientID + "_" + this.ClientID + ":expanded");
                    if (Strings.Len(clientVariable) > 0)
                    {
                        obj2 = BooleanType.FromString(clientVariable);
                    }
                    else
                    {
                        obj2 = BooleanType.FromString(this.get_CustomAttribute("expanded", "0"));
                    }
                }
                else
                {
                    obj2 = BooleanType.FromString(this.get_CustomAttribute("expanded", "0"));
                }
                return BooleanType.FromObject(obj2);
            }
        }

        public bool IsTrackingViewState
        {
            get
            {
                return false;
            }
        }

        public string LeftHTML
        {
            get
            {
                return this.get_CustomAttribute("lhtml", "");
            }
            set
            {
                this.set_CustomAttribute("lhtml", value);
            }
        }

        private ITreeNodeWriter NodeWriter
        {
            get
            {
                if (this.m_objDNNTree.IsDownLevel)
                {
                    return new TreeNodeWriter();
                }
                return null;
            }
        }

        [Browsable(false)]
        public TreeNode Parent
        {
            get
            {
                return this.ParentNode;
            }
        }

        public TreeNode ParentNode
        {
            get
            {
                if ((this.XmlNode.ParentNode != null) && (this.XmlNode.ParentNode.NodeType != XmlNodeType.Document))
                {
                    return new TreeNode(this.XmlNode.ParentNode, this.m_objDNNTree);
                }
                return null;
            }
        }

        public string RightHTML
        {
            get
            {
                return this.get_CustomAttribute("rhtml", "");
            }
            set
            {
                this.set_CustomAttribute("rhtml", value);
            }
        }

        //public bool System.Web.UI.IStateManager.IsTrackingViewState
        //{
        //    get
        //    {
        //        return false;
        //    }
        //}

        [PersistenceMode(PersistenceMode.InnerProperty), Browsable(true)]
        public TreeNodeCollection TreeNodes
        {
            get
            {
                if (this.m_objNodes == null)
                {
                    this.m_objNodes = new TreeNodeCollection(this.XmlNode, this.DNNTree);
                }
                return this.m_objNodes;
            }
        }
    }
}

